package com.Impl;

import com.dao.BaseDao;
import com.dao.collect_productDao;
import com.entity.collect_product;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class collect_productDaoImpl extends BaseDao implements collect_productDao {
  public collect_productDaoImpl(Connection con) {
    super(con);
  }

  @Override
  public List<collect_product> findALl() {
    List<collect_product> list = new ArrayList<>();
    String sql = "select * from collect_product";
    ResultSet rs = this.executeQuery(sql,null);
    try {
      while(rs.next()){
        collect_product collect_product = new collect_product();
        collect_product.setCid(rs.getInt("cid"));
        collect_product.setBid(rs.getInt("bid"));
        collect_product.setCname(rs.getString("cname"));
        collect_product.setCprice(rs.getDouble("cprice"));
        collect_product.setCpathPic(rs.getString("cpathPic"));
        collect_product.setCbrief(rs.getString("cbrief"));
        list.add(collect_product);
      }
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return list;
  }

  @Override
  public collect_product findByIdOne(int cid) {
    collect_product collect_product = new collect_product();
    String sql = "select * from collect_product where cid=?";
    Object[] param = {cid};
    ResultSet rs = this.executeQuery(sql,param);
    try {
      while(rs.next()){
        collect_product.setCid(rs.getInt("cid"));
        collect_product.setBid(rs.getInt("bid"));
        collect_product.setCname(rs.getString("cname"));
        collect_product.setCprice(rs.getDouble("cprice"));
        collect_product.setCpathPic(rs.getString("cpathPic"));
        collect_product.setCbrief(rs.getString("cbrief"));
      }
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return collect_product;
  }

  @Override
  public List<collect_product> findByBidAll(int bid) {
    List<collect_product> list = new ArrayList<>();
    String sql = "select * from collect_product where bid=?";
    Object[] param = {bid};
    ResultSet rs = this.executeQuery(sql,param);
    try {
      while(rs.next()){
        collect_product collect_product = new collect_product();
        collect_product.setCid(rs.getInt("cid"));
        collect_product.setBid(rs.getInt("bid"));
        collect_product.setCname(rs.getString("cname"));
        collect_product.setCprice(rs.getDouble("cprice"));
        collect_product.setCpathPic(rs.getString("cpathPic"));
        collect_product.setCbrief(rs.getString("cbrief"));
        list.add(collect_product);
      }
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return list;
  }

  @Override
  public boolean addCollect_Product(collect_product collect_product) {
    boolean flag = false;
    String sql = "insert into collect_product(bid,cname,cbrief,cprice,cpathPic) values(?,?,?,?,?)";
    Object[] param = {collect_product.getBid(),collect_product.getCname(),collect_product.getCbrief(),collect_product.getCprice(),collect_product.getCpathPic()};
    int result = this.executeUpdate(sql,param);
    if(result>0){
      flag = true;
    }
    return flag;
  }

  @Override
  public int checkName(String cname) {
    int result = 0;
    String sql = "select count(*) from collect_product where cname=?";
    Object[] param = {cname};
    ResultSet rs = this.executeQuery(sql,param);
    try {
      if(rs.next()){
        result = rs.getInt(1);
        System.out.println("result:"+result);
//        System.out.println("str:"+str);
//        flag = false;
//      }else{
//        flag = true;
      }
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return result;
  }
}
